const app = getApp();
Page({
  data: {
    userid: '',
    username: '',
    department: '',
    equipmentId: '',
    equipmentName: '',
    equipmentAddress: '',
    errorMemo: '',
    key: '',
    disabledSubmit: true,
    objectArray: [],
    arrIndex: -1,
    equipmentTypeId: '',
    equipmentTypeName: '',
    count: 0,
  },
  onLoad(query) {
    // 页面加载
    dd.showLoading({
      content: '加载中...',
      delay: 1000,
    });
    var that = this;
    that.setData({
      key: app.globalData.key
    });

    dd.getAuthCode({
      success: (res) => {
        dd.httpRequest({
          url: 'http://www.ejzcat.com/DingApi/DingTalkLib/Api/GetUserInfo.ashx',
          method: 'POST',
          data: {
            key: app.globalData.key,
            code: res.authCode
          },
          dataType: 'json',
          async: false,
          success: function (res2) {
            that.setData({
              userid: res2.data.userid,
              username: res2.data.username,
              department: res2.data.department
            });
            app.globalData.userid = res2.data.userid;
            app.globalData.username = res2.data.username;
            app.globalData.department = res2.data.department;
            dd.httpRequest({
              url: 'http://www.ejzcat.com/dingapi/equipmentrepair/getequipmenttype.ashx',
              method: 'POST',
              data: {
                key: app.globalData.key
              },
              dataType: 'json',
              async: false,
              success: function (res3) {
                dd.hideLoading();
                that.setData({
                  objectArray: res3.data.Items
                });
                that.onShow();
              },
              fail: function (err) {
                dd.alert({ content: JSON.stringify(err) })
              }
            })
          },
          fail: function (err) {
            dd.alert({ title: JSON.stringify(err) });
          }
        });
      },
      fail: (err) => {
        dd.alert({ content: JSON.stringify(err) })
      }
    });
  },
  onReady() {
    // 页面加载完成
  },
  onShow() {
    if (app.globalData.userid == '') {
      return;
    }
    // 页面显示
    dd.showLoading({
      content: '加载中...',
      delay: 1000,
    });
    var that = this;
    dd.httpRequest({
      url: 'http://www.ejzcat.com/DingApi/EquipmentRepair/Query.ashx',
      method: 'POST',
      data: {
        status: 0,
        userid: app.globalData.userid
      },
      dataType: 'json',
      async: false,
      success: function (res) {
        dd.hideLoading();
        that.setData({
          count: res.data.Items.length
        });
      },
      fail: function (err) {
        dd.alert({ content: JSON.stringify(err) });
      }
    });
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  onShareAppMessage() {
    // 返回自定义分享信息
  },
  bindObjPickerChange(e) {
    this.setData({
      arrIndex: e.detail.value,
      equipmentTypeId: this.data.objectArray[e.detail.value].id,
      equipmentTypeName: this.data.objectArray[e.detail.value].name
    });
    this.set_disabledSubmit();
  },
  showScan() {
    dd.scan({
      type: 'qr',
      success: (res) => {
        this.setData({
          equipmentId: res.code
        });
      },
    });
  },
  equipmentNameInput(e) {
    this.setData({
      equipmentName: e.detail.value
    });
    this.set_disabledSubmit();
  },
  equipmentAddressInput(e) {
    this.setData({
      equipmentAddress: e.detail.value
    });
    this.set_disabledSubmit();
  },
  errMemoInput(e) {
    this.setData({
      errorMemo: e.detail.value
    });
    this.set_disabledSubmit();
  },
  set_disabledSubmit() {
    var that = this;
    if (that.data.equipmentName != '' && that.data.equipmentAddress != '' && that.data.errorMemo != '' && that.data.arrIndex != -1) {
      that.setData({
        disabledSubmit: false
      });
    }
    else {
      that.setData({
        disabledSubmit: true
      });
    }
  },
  onSubmit() {
    var that = this;
    if (that.data.equipmentName == '') {
      dd.alert({ content: '设备名称不能为空' });
      return;
    }
    if (that.data.equipmentAddress == '') {
      dd.alert({ content: '设备地点不能为空' });
      return;
    }
    if (that.data.errorMemo == '') {
      dd.alert({ content: '故障描述不能为空' });
      return;
    }
    dd.showLoading({
      content: '数据提交中...',
      delay: 1000,
    });
    dd.httpRequest({
      url: 'http://www.ejzcat.com/DingApi/EquipmentRepair/RepairSubmit.ashx',
      method: 'POST',
      data: that.data,
      dataType: 'text',
      success: function (res) {
        dd.hideLoading();
        if (res.data == 1) {
          that.setData({
            equipmentId: '',
            equipmentName: '',
            equipmentAddress: '',
            errorMemo: ''
          });
          dd.alert({ content: '提交成功' });
        }
        else {
          dd.alert({ content: '提交失败:' + res.data });
        }
      },
      fail: function (err) {
        dd.alert({ content: JSON.stringify(err) });
      },
      complete: function (res) {

      }
    });
  },
  switchTab_evaluate() {
    dd.switchTab({
      url: '/mypages/equipment/evaluate/index'
    })
  },
});
